package com.acme.bankapp.service.bank;

public class CheckingAccount extends AbstractAccount {
    private double overdraft;

    public CheckingAccount(double balance, double overdraft) {
        super(balance);
        if (overdraft < 0) {
            throw new IllegalArgumentException("Balance cannot be less than 0.");
        }
        this.overdraft = overdraft;
    }

    @Override
    public void withdraw(double x) throws OverDraftLimitExceededException {
        if (x > maximumAmountToWithdraw()) {
            throw new OverDraftLimitExceededException(getBalance(), maximumAmountToWithdraw());
        }
        if (getBalance() >= x) {
            setBalance(getBalance() - x);
        } else if (getBalance() + overdraft >= x) {
            x -= getBalance();
            setBalance(0);
            overdraft -= x;
        }
    }

    @Override
    public double maximumAmountToWithdraw() {
        return getBalance() + overdraft;
    }
}